//
//  SearchLostViewController.m
//  Find
//
//  Created by lanou3g on 15/11/5.
//  Copyright © 2015年 Reiko. All rights reserved.
//

#import "SearchLostViewController.h"

@interface SearchLostViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableArray *dataArray;
@end

@implementation SearchLostViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self addTableView];
        self.dataArray = [NSMutableArray array];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(actionSearch1:) name:@"SEARCHLOST" object:nil];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self.tableView reloadData];
    
    //[self.tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
  //  [self addTableView];
   
}
- (void)actionSearch1:(NSNotification *)notification
{
    
    //self.dataArray = [NSMutableArray array];
    self.dataArray = [notification.object valueForKey:@"array"];
    [self.tableView reloadData];

}

- (void)addTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
  //  UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"faye2"]];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  //  self.tableView.backgroundView = imageView;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
}

#pragma mark === tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

// 每个分区的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    return 130;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * identifier = @"lostViewController";
    LostListTableViewCell * listcell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (listcell == nil) {
        listcell = [[LostListTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }
    listcell.backgroundColor = [UIColor whiteColor];
    LostMessageModel *lostModel = self.dataArray[indexPath.row];
    listcell.backgroundColor = [UIColor clearColor];
    
    
    AVUser *user1 = lostModel.user;
    //    AVUser *user2 = [AVQuery getUserObjectWithId:user1.objectId];  // 同步请求数据导致界面卡顿
    AVQuery * query = [AVQuery queryWithClassName:@"_User"];
    [query  getObjectInBackgroundWithId:user1.objectId block:^(AVObject *object, NSError *error) {
        NSString *urlString = [[object valueForKey:@"photoImage"] getThumbnailURLWithScaleToFit:NO width:100 height:100];
        [listcell.listImage sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
    }];
    
    // 用户名
    listcell.listTextLabel.text = lostModel.context;
    // 上传的日期
    NSDateFormatter *dateFoematter = [[NSDateFormatter alloc] init];
    [dateFoematter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSRange range = NSMakeRange(5, 11);
    listcell.listTimeLabel.text = [[dateFoematter stringFromDate: lostModel.createdAt] substringWithRange:range];
    // 距离
    listcell.listAddressLabel.text = lostModel.location;
    
    return listcell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *objectID = [self.dataArray[indexPath.row] objectId];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"FIND" object:@{@"objectID":objectID,@"flag":@"lost"}];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
